package com.ysd.lis.service.impl;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.yulichang.wrapper.MPJLambdaWrapper;
import com.ysd.common.Result;
import com.ysd.lis.dto.InterfaceDto;
import com.ysd.lis.entity.SysInterface;
import com.ysd.lis.entity.SysInterfaceDict;
import com.ysd.lis.entity.SysInterfaceMethod;
import com.ysd.lis.mapper.SysInterfaceDictMapper;
import com.ysd.lis.mapper.sys.SysInterfaceMethodMapper;
import com.ysd.lis.request.InterfaceParam;
import com.ysd.lis.service.SysBasDictDetailService;
import com.ysd.lis.service.SysInterfaceMethodService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * <p>
 * cold_chain.t_user 服务实现类
 * </p>
 *
 * @author peng
 * @since 2022-04-24
 */
@Service
@Slf4j
public class SysInterfaceMethodServiceImpl extends ServiceImpl<SysInterfaceMethodMapper, SysInterfaceMethod> implements SysInterfaceMethodService {

    @Autowired
    SysInterfaceMethodMapper sysInterfaceMethodMapper;

    @Autowired
    SysBasDictDetailService sysBasDictDetailService;

    @Autowired
    SysInterfaceDictMapper sysInterfaceDictMapper;


    @Override
    public Result findInterfaceMethodList(InterfaceParam param) {
        Map returnMap = new HashMap();
        MPJLambdaWrapper<SysInterfaceMethod> queryWrapper = new MPJLambdaWrapper<>();
        queryWrapper.selectAll(SysInterfaceMethod.class)
                .selectAs("sa", SysInterfaceDict::getName, SysInterfaceMethod::getInterfaceDictName)
                .selectAs("sa", SysInterfaceDict::getTypeName, SysInterfaceMethod::getTypeName)
                .selectAs("sa", SysInterfaceDict::getSqlType, SysInterfaceMethod::getSqlType)
                .selectAs("sa", SysInterfaceDict::getInterfaceGroupName, SysInterfaceMethod::getInterfaceGroup)
                .selectAs("sa", SysInterfaceDict::getOrderCode, SysInterfaceMethod::getOrderCode)
                .selectAs("sa", SysInterfaceDict::getCode, SysInterfaceMethod::getDictCode)
                .selectAs("si", SysInterface::getDbMs, SysInterfaceMethod::getDbMs)
                .leftJoin(SysInterfaceDict.class, "sa", SysInterfaceDict::getId, SysInterfaceMethod::getInterfaceDictId)
                .leftJoin(SysInterface.class, "si", SysInterface::getId, SysInterfaceMethod::getInterfaceId)
                .eq(SysInterfaceMethod::getDelFlag, 0)
                .like(StrUtil.isNotEmpty(param.getModuleId()),SysInterfaceDict::getOwnSystem, param.getModuleId())
                .like(StrUtil.isNotEmpty(param.getSystemId()),SysInterfaceDict::getInterfaceGroup, param.getSystemId())
                .like(StrUtil.isNotEmpty(param.getTypeId()),SysInterfaceDict::getType,param.getTypeId());
        if (StringUtils.isNotBlank(param.getInterfaceId())) {
            queryWrapper.eq(SysInterfaceMethod::getInterfaceId, param.getInterfaceId());
        }
        queryWrapper.orderByAsc(SysInterfaceDict::getInterfaceGroup);
        queryWrapper.orderByAsc(SysInterfaceMethod::getSeq);
        queryWrapper.orderByAsc(SysInterfaceDict::getOrderCode);
        List<SysInterfaceMethod> detailList = sysInterfaceMethodMapper.selectList(queryWrapper);

        LambdaQueryWrapper<SysInterfaceDict> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper
                .like(StrUtil.isNotEmpty(param.getModuleId()),SysInterfaceDict::getOwnSystem,param.getModuleId())
                .like(StrUtil.isNotEmpty(param.getSystemId()),SysInterfaceDict::getInterfaceGroup,param.getSystemId())
                .like(StrUtil.isNotEmpty(param.getTypeId()),SysInterfaceDict::getType,param.getTypeId())
                .eq(SysInterfaceDict::getDelFlag,0);
        List<String> collect = detailList.stream().map(SysInterfaceMethod::getInterfaceDictId).collect(Collectors.toList());
        if(CollectionUtils.isNotEmpty(collect)){
            lambdaQueryWrapper.notIn(SysInterfaceDict::getId,collect);
        }
        List<SysInterfaceDict> sysInterfaceDicts = sysInterfaceDictMapper.selectList(lambdaQueryWrapper);

//        List<SysInterfaceMethod> detailList = detailLists.stream().filter(a->a.getIsSet()==2).collect(Collectors.toList());
//        List<SysInterfaceMethod> sysInterfaceDicts = detailLists.stream().filter(a->a.getIsSet()==1).collect(Collectors.toList());

        returnMap.put("detailList",detailList);
        returnMap.put("dictList",sysInterfaceDicts);
        return Result.succ(1, "查询成功", returnMap);
    }

    @Override
    public Result saveOrUpdateInterfaceMethod(InterfaceDto dto) {
        List<SysInterfaceMethod> methodList = dto.getMethodList();
        boolean b = this.saveOrUpdateBatch(methodList);
        if (b) {
            return Result.succ(1, "成功", null);
        } else {
            return Result.fail(400, "失败", "");
        }
    }

    @Override
    public Result deleteBatchInterfaceMethod(InterfaceDto dto) {
        List<SysInterfaceMethod> methodList = dto.getMethodList();
        List<String> ids = methodList.stream().map(SysInterfaceMethod::getId).collect(Collectors.toList());
        boolean b = this.removeByIds(ids);
        if (b) {
            return Result.succ(1, "删除成功", null);
        } else {
            return Result.fail(400, "删除失败", "");
        }
    }

}
